package com.dinstar.dinlink.ui;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.dinstar.dinlink.databinding.ActivityCallIncomingBinding;
import com.dinstar.linphone.BaseActivity;
import com.dinstar.linphone.core.LinphoneManager;

import org.linphone.core.Call;

public class CallIncomingActivity extends BaseActivity {

    private ActivityCallIncomingBinding callIncomingBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callIncomingBinding = ActivityCallIncomingBinding.inflate(getLayoutInflater());
        setContentView(callIncomingBinding.getRoot());
        setCurrentActivity(this);
        initView();
    }


    private void initView() {

        Call currentCall = LinphoneManager.getInstance().getCurrentCall();
        String displayName = currentCall.getRemoteAddress().getDisplayName();
        callIncomingBinding.callDisplayName.setText(displayName);

        Glide.with(this)
                .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201712%2F10%2F20171210134053_rMszF.thumb.700_0.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1722393070&t=0d7923c0909e4fc04b71fadceb54440f")
                .apply(new RequestOptions().transform(new RoundedCorners(10)))
                .into(callIncomingBinding.callHeader);

        callIncomingBinding.answer.setOnClickListener(v -> {
            int state = LinphoneManager.getInstance().answerCall(currentCall);
            if (state == 0) {
                finish();
            }
        });

        callIncomingBinding.hangUp.setOnClickListener(v -> {
            int state = LinphoneManager.getInstance().declineCall(currentCall);
            if (state == 0) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeCurrentActivity(this);
    }
}
